import { selectCuesActive } from "../../../../service/api/api-cues-active";
import { attachmentPreview } from "../../../../service/api/api-vues-attachment";

// pages/cues/active/details/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicId: 0,//当前选题线索的id
    activeDetails: {},//选题线索详情
    DetailDrawerVisible: false,//上报线索详细信息是否显示
    index: 0,//当前详情在上页列表中的位置
    previewData: {},//预览图片信息
    previewDataUrlType: 'preview',
    imageList: [],//现有的图片列表
    viewPreviewImageVisible: false,//是否预览图片
    activeAssignOrganizations: false,//指派征集单位是否显示
  },
  //预览图片
  async onClickPreviewImage(e) {
    const file = e.currentTarget.dataset.file
    const type = e.currentTarget.dataset.type
    if (!type.includes('image')) {
      const response = await attachmentPreview({
        id: file.id,
      });
      if (response.code === 0) {
        this.setData({
          previewData: {
            title: file.name,
            onlinePreview: true,
            url: file.content,
            content: response.data,
          }
        })
        wx.navigateTo({
          url: '/pages/web/index?url=' + encodeURIComponent(this.data.previewData.content) + '&title=' + this.data.previewData.title + '&type=' + this.data.previewDataUrlType
        })
      } else {
        wx.showToast({
          title: '暂时无法预览',
          icon: 'error'
        })
      }
    } else {
      // const List = this.data.activeDetails.articleAttachments.filter((item: any) => item.mimeType.includes("image"));
      // const imageList = List.map((item) => item.content)
      const List = this.data.activeDetails.articleAttachments.filter((item: any) => {
        if (item.mimeType !== null) {
          return item.mimeType.includes("image")
        } else {
          return
        }
      });
      const imageList = List.map((item) => item.content)
      this.setData({
        imageList,
        viewPreviewImageVisible: true
      })
    }
  },
  //关闭图片预览
  onClosePreviewImage(e) {
    this.setData({
      viewPreviewImageVisible: false,
    });
  },
  //下载图片
  onDownloadFile(e) {
    const file = e.currentTarget.dataset.file
    wx.downloadFile({
      url: file.content,
      success: function (res) {
        // wx.showToast({
        //   title: '下载成功',
        //   icon: 'success',
        // })
        if (res.statusCode === 200) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true, //关键点
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '下载失败',
          icon: 'error'
        })
      }
    })
  },
  //打开详细信息抽屉
  onOpenDetailDrawer() {
    this.setData({
      DetailDrawerVisible: true
    })
  },
  //拨打电话
  makePhoneCall(e: any) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
    })
  },
   //处理附件类型
   readyAttachments(list: any) {
    const newList = list.map((item) =>{
      const type = item.name.match(/\.([^\.]+)$/)[1]
      if(type==='doc'||type==='docx'||type==='xls'||type==='xlsx'||type==='ppt'||type==='pptx'||type==='pdf'){
        item.showDownload=true
      }else{
        item.showDownload=false
      }
      return item
    })
  },
  /**
  * 加载 选题线索详情
  * @param key
  */
  async loadCuesActiveDetails(id) {
    wx.showLoading({
      title: "加载中..."
    })
    try {
      const response = await selectCuesActive({ id: id });
      const { code, data } = response;
      if (code === 0) {
        this.readyAttachments(data.articleAttachments)
        this.setData({
          activeDetails: data
        })
        wx.hideLoading()
      }
    } catch (error) { }
  },
  //开关指派征集单位
  handleAssignOrganizationsChange() {
    this.setData({
      activeAssignOrganizations: true,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: { topicId: any; index: any; }) {
    this.setData({
      topicId: options.topicId,
      index: options.index
    })
    this.loadCuesActiveDetails(options.topicId)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setStorageSync('active-page', 'detail')
    wx.setStorageSync('active-detail-index', this.data.index)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})